//
//  CalculatorView.m
//  MoneyIndividual
//
//  Created by Tai Le on 12/30/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "CalculatorView.h"

@implementation CalculatorView

- (id)initWithTextField:(UITextField *)textField
{
    self = [super init];
    if (self)
    {
        _buttons = [[NSMutableArray alloc] init];
        _lines = [[NSMutableArray alloc] init];
        
        _textField = textField;
        
        for (int i = ComputeC; i <= ComputeEqual; i++)
        {
            UIButton *button = nil;
            if (i == ComputeC)
            {
                button = [self newButtonWithTag:i title:@"c" normalColor:COLOR_RED highlightedColor:COLOR_RED_DARK];
            }
            else if (i == ComputeAdd)
            {
                button = [self newButtonWithTag:i title:@"+" normalColor:COLOR_GRAY highlightedColor:COLOR_GRAY_DARK];
            }
            else if (i == ComputeSubtract)
            {
                button = [self newButtonWithTag:i title:@"-" normalColor:COLOR_GRAY highlightedColor:COLOR_GRAY_DARK];
            }
            else if (i == ComputeMulti)
            {
                button = [self newButtonWithTag:i title:@"x" normalColor:COLOR_GRAY highlightedColor:COLOR_GRAY_DARK];
            }
            else if (i == ComputeDivide)
            {
                button = [self newButtonWithTag:i title:@"÷" normalColor:COLOR_GRAY highlightedColor:COLOR_GRAY_DARK];
            }
            else if (i == ComputeEqual)
            {
                button = [self newButtonWithTag:i title:@"=" normalColor:COLOR_GREEN highlightedColor:COLOR_GREEN_DARK];
            }
            
            if (button == nil)
            {
                continue;
            }
            
            [_buttons addObject:button];
            
            if (i > ComputeC)
            {
                UIView *lineView = [UIView newLineView];
                [self addSubview:lineView];
                [_lines addObject:lineView];
            }
        }
        
        self.tintColor = COLOR_WHITE_PURE;
    }
    return self;
}

- (UIButton*)newButtonWithTag:(int)tag title:(NSString*)title normalColor:(UIColor*)normalColor highlightedColor:(UIColor*)highlightedColor
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:COLOR_WHITE_PURE forState:UIControlStateNormal];
    button.titleLabel.font = FONT(24);
    button.tag = tag;
    [button setBackgroundImage:[UIImage imageFromColor:normalColor] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageFromColor:highlightedColor] forState:UIControlStateHighlighted];
    [button setBackgroundImage:[UIImage imageFromColor:highlightedColor] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    return button;
}

- (void)startupWithResult:(float)result
{
    _result = result;
    _textField.text = @"";
    _compute = ComputeNone;
    [self calculateCurrentValue];
    [self highlightedButton:nil];
}

- (void)calculateCurrentValue
{
    if (_compute == ComputeNone)
    {
        _result = [_textField floatValue];
    }
    else
    {
        if (_state == TextFieldStateInputing)
        {
            if (_compute == ComputeAdd)
            {
                _result += [_textField floatValue];
            }
            else if (_compute == ComputeSubtract)
            {
                _result -= [_textField floatValue];
            }
            else if (_compute == ComputeMulti)
            {
                _result *= [_textField floatValue];
            }
            else if (_compute == ComputeDivide)
            {
                if ([_textField floatValue] == 0)
                {
                    _result = 0;
                }
                else
                {
                    _result /= [_textField floatValue];
                }
            }
        }
    }
    
    _textField.text = [NSString stringWithFormat:@"%0.0f",_result];
}

- (void)highlightedButton:(UIButton*)highlightedButton
{
    for (UIButton *button in _buttons)
    {
        [button setSelected:NO];
        
        if ([button isEqual:highlightedButton])
        {
            [button setSelected:YES];
        }
    }
}

#pragma mark - compile source

- (void)buttonPressed:(UIButton*)button
{
    [self calculateCurrentValue];
    
    [self highlightedButton:button];
    
    int i = button.tag;
    _compute = i;
    
    if (i == ComputeEqual)
    {
        _compute = ComputeNone;
        [button setSelected:NO];
    }
    else if (_compute == ComputeC)
    {
        [self startupWithResult:0];
    }
    
    if ([_calculatorDelegate respondsToSelector:@selector(calculator:didPressedComput:)])
    {
        [_calculatorDelegate calculator:self didPressedComput:i];
    }
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    
    int lineWid = 1;
    float wid = (self.width - _lines.count*lineWid) /_buttons.count;
    float left = 0;
    
    for (int i = 0; i < _buttons.count; i++)
    {
        UIButton *button = [_buttons objectAtIndex:i];
        button.frame = RECT(left, 0, wid, self.height);
        
        left = button.right;
        
        if (i < _lines.count)
        {
            UIView *lineView = [_lines objectAtIndex:i];
            lineView.frame = RECT(left, 0, lineWid, self.height);
            left = lineView.right;
        }
    }
}


@end
